﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Itop.YHPC
{
    public partial class UploadS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strJSON = string.Empty;
            string[] strExten = { ".jpg", ".bmp", ".gif", ".png" };
            Session["UserName"] = "zack";
            string strUserName = Session["UserName"].ToString();
            string recordid = Request["recordid"];
            string imagetype = Request["imagetype"];

            bool judge = false;

            try
            {
                if (Request.Files.Count > 0)
                {
                    HttpPostedFile postedFile = Request.Files[0];

                    foreach (string strExtension in strExten)
                    {

                        if (Path.GetExtension(postedFile.FileName).ToLower() == strExtension)
                        {

                            string savePath = Server.MapPath("~\\Upload\\");
                           // string savePath = "../Upload/";

                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            if (!string.IsNullOrEmpty(recordid) && !string.IsNullOrEmpty(imagetype))
                            {
                                string pictype = imagetype + recordid + "_";
                                DirectoryInfo di = new DirectoryInfo(savePath);

                                FileInfo[] fi = di.GetFiles("*"+strExtension);

                                foreach (FileInfo f in fi)
                                {
                                    if (f.Name.Contains(pictype))
                                    {
                                        f.Delete();
                                    }
                                }
                                //删掉其他后缀的相同类型的图片
                                foreach (string Extension in strExten)
                                {
                                    if (!Extension.Equals(strExtension))
                                    {
                                        FileInfo[] fi1 = di.GetFiles("*" + Extension);

                                        foreach (FileInfo f in fi1)
                                        {
                                            if (f.Name.Contains(pictype))
                                            {
                                                f.Delete();
                                            }
                                        }
                                    }
                                }
                                savePath += pictype + Path.GetFileName(postedFile.FileName);
                            }
                            else
                            {
                                savePath += strUserName + "_" + Path.GetFileName(postedFile.FileName);
                                //检查是否在服务器上已经存在用户上传的同名文件
                                if (File.Exists(savePath))
                                {
                                    File.Delete(savePath);
                                }

                            }


                            postedFile.SaveAs(savePath);

                            strJSON = "{success:true,message:'上传成功',path:'" + savePath + "'}";

                            judge = true;

                            break;
                        }
                    }

                    if (!judge)
                        strJSON = "{success:false,message:'上传失败,原因:仅支持图片上传！'}";
                }
            }
            catch (Exception ex)
            {
                strJSON = "{success:false,message:'上传失败,原因:" + ex.Message.ToString() + "'}";
            }

            //在这里请你注意删除你对应页面的所有 html 脚本,只需要保留 page 头就可以
            Response.Write(strJSON);
            //Response.Flush();
        }
    }
}
